package com.lnsf.controller;
import java.io.File;
import java.io.IOException;
import java.security.spec.DSAGenParameterSpec;
import java.util.List;
/**
 * @author ddxing
 * @time 2017-7-26
 */
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.sql.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import com.lnsf.entity.Goods;
import com.lnsf.service.IGoodsService;

@Controller
@RequestMapping("/goods")
public class GoodsController {
	
	@Autowired
	private IGoodsService igoodsService;
	
	@RequestMapping(value="/deleteGoods/{id}")
	@ResponseBody
	public String deletegoods(@PathVariable("id") Integer id){
		igoodsService.delete(id);
		System.out.println("deletegoods success！！");
		return "success";
	}
	
	@RequestMapping(value="/updateGoods")
	@ResponseBody
	public String updategoods(int id,String name){
		int result=igoodsService.update(id, name);
		System.out.println("updategoods ："+result);
		return "success";
	}
	
	@RequestMapping(value="/saveGoods")
	@ResponseBody
	public String savegoods(Goods goods){		
		Goods goods1 = igoodsService.save(goods);
		System.out.println("savegoods :"+goods1);
		return "success";	
	}
	/*
	 * 获得商品详情，用json返回
	 * 2017-7-29
	 */
	@RequestMapping("/findGoods/{id}")
	@ResponseBody
	public Goods findGoods(@PathVariable("id") int id){		
		Goods goods = igoodsService.findOne(id);
		System.out.println("findGoods :"+goods);
		return goods;
	}
	
	@RequestMapping("/findAllGoods")
	@ResponseBody
	public Page<Goods> list(@RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr, 
			Map<String, Object> map){
		int pageNo = 1;
		try {
			pageNo = Integer.parseInt(pageNoStr);
			if(pageNo < 1){
				pageNo = 1;
		}
		} catch (Exception e) {}
		
		Page<Goods> page = igoodsService.getPage(pageNo, 5);
		map.put("page", page);
		
		System.out.println("test goods ->:"+page.getTotalElements());
		
		return page;
	}
	
	
	
	//发布商品，上传照片
	@RequestMapping("/saveGoods")
	@ResponseBody
	public String saveProduct(HttpServletRequest servletRequest ,
						@ModelAttribute Goods goods, MultipartFile img) throws IOException {
		
		//存储路径 ,记得设置虚拟路径
		//String path = servletRequest.getSession().getServletContext().getRealPath("pic"); 
		String path = "E:\\tomcat7_pic";
		String fileName = img.getOriginalFilename();
        if (fileName.endsWith("jpg") || fileName.endsWith("png")) {
            File file = new File(path, fileName);
            System.out.println(file.getAbsolutePath());
            img.transferTo(file);
            goods.setImage(fileName);
            //保存商品
            igoodsService.save(goods);
            return "success";
        }
        else{
        	return "false";
        }
	
	}


}
